<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme

export default {
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
    unit: {
      type: String,
      default: "台",
    },
    dataName: {
      type: String,
      default: "容量",
    },
    fdzxsX: {
      type: Array,
      default: () => []
    },
    fdzxsD: {
      type: Array,
      default: () => []
    },
    fdzxsD1: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      chart: null,
      dataList: [120, 200, 150, 80, 70, 110, 130],
    };
  },
  watch: {
    fdzxsX: {
      handler() {
        this.updateChart();
      },
      deep: true
    },
    fdzxsD: {
      handler() {
        this.updateChart();
      },
      deep: true
    },
    fdzxsD1: {
      handler() {
        this.updateChart();
      },
      deep: true
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.updateChart();
    },
    updateChart() {

      let xAxisData = this.fdzxsX;
      let seriesData = this.fdzxsD;
      let seriesData1 = this.fdzxsD1;
      this.chart.setOption({
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          }
        },
        legend: {
          data: ["电量", "等效时"],
          textStyle: {
            color: "#ffffff",
          },
          right: 0,
        },
        grid: {
          top: 30,
          left: 45,
          right: 0,
          bottom: 25,
        },
        xAxis: [
          {
            type: "category",
            axisTick: { show: false },
            data: xAxisData,
          },
        ],
        yAxis:
        {
          type: "value",
          splitLine: {
            lineStyle: {
              color: "#B0D7FF33",
            },
          },
          // 添加格式化显示
          axisLabel: {
            formatter: function (value) {
              // 将数值除以10000并添加单位
              return value / 10000;
            }
          },
          name: `单位：${this.unit}`,
          nameTextStyle: {
            color: "#999999",
          },
        },

        series: [
          {
            barWidth: '20',
            name: "电量",
            type: "bar",
            emphasis: {
              focus: "series",
            },
            data: seriesData,
          },
          {
            barWidth: '20',
            name: "等效时",
            type: "bar",
            emphasis: {
              focus: "series",
            },
            data: seriesData1,
          },
        ],
      });
    },
  },
};
</script>
